package top.hmtools.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;

import top.hmtools.RLContext;
import top.hmtools.common.StaticContent;
import top.hmtools.pojo.RequestLogBean;
import top.hmtools.rlDao.RequestLogDao;

/**
 * 执行写入请求日志信息到数据库的service实现类
 * @author HyboJ
 *
 */
@Service
@ConditionalOnProperty(prefix=StaticContent.CONFIG_PREFIX,value=StaticContent.CONFIG_ITEM_KEY_NAME_ENABLED,matchIfMissing=true)
public class RequestLoggerServiceImpl implements IRequestLoggerService {
	
	private static Logger logger = LoggerFactory.getLogger(RequestLoggerServiceImpl.class);
	
	@Autowired
	private RLContext rlContext;

	@Override
	public int addLogs(List<RequestLogBean> logBeans) {
		int result = 0;
		SqlSession sqlSession = this.rlContext.getSqlSession();
		try {
			RequestLogDao logDao =sqlSession.getMapper(RequestLogDao.class);
			if(logDao!=null){
				result = logDao.addLogs(logBeans);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(),e);
		}finally{
			sqlSession.close();
		}
		return result;
	}

	@Override
	public int addLogsExtraInfos(List<RequestLogBean> logBeans) {
		int result = 0;
		SqlSession sqlSession = this.rlContext.getSqlSession();
		try {
			RequestLogDao logDao =sqlSession.getMapper(RequestLogDao.class);
			if(logDao!=null){
				result = logDao.addLogsExtraInfos(logBeans);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(),e);
		}finally{
			sqlSession.close();
		}
		return result;
	}

	@Override
	public int addLogs(RequestLogBean... logBeans) {
		List<RequestLogBean> beans = null;
		int result = 0;
		if(logBeans != null){
			beans = new ArrayList<RequestLogBean>();
			for(RequestLogBean tmp:logBeans){
				beans.add(tmp);
			}
			result = this.addLogs(beans);
		}
		return result;
	}

	@Override
	public int addLogsExtraInfos(RequestLogBean... requestLogBeans) {
		int result = 0;
		if(requestLogBeans != null){
			List<RequestLogBean> beans = new ArrayList<RequestLogBean>();
			for(RequestLogBean tmp:requestLogBeans){
				beans.add(tmp);
			}
			result = this.addLogsExtraInfos(beans);
		}
		return result;
	}

}
